/**
 * @Author: LiMeng
 * @Date: 2017/5/12
 */
var GLOBAL = {
    ID: $.getQueryParam("id"),
    ACTION_URL: ROOT_PATH + '/securityCheck/checkPlan/edit.shtml'
};
var handleNameChange = function () {
    debugger;
    var $checkTypeName = $('#checkTypeId').combobox('getText');
    var $checkDeptIdNames = $('#checkDeptIds').combotree('getText');
    var $planStartDate = $('#planStartDate').datebox('getValue');
    $('#name').textbox('setValue', $checkDeptIdNames + $planStartDate + $checkTypeName);
};
var ajaxSuccessHandler = function (result) {
    result = $.parseJSON(result);
    if (result.success) {
        $.messager.progress('close');	// 如果提交成功则隐藏进度条
        layer.msg(result.message, {time: 1000, icon: 1}, function () {
            WindowManager.closeLayer({returnValue: true});
        });
    } else {
        $.messager.progress('close');	// 如果提交成功则隐藏进度条
        layer.msg(result.message, {icon: 2});
    }
};
var addCommonFormParams = function (param) {
    param.checkTypeName = $('#checkTypeId').combo('getText');
    param.checkLevelName = $('#checkLevelId').combo('getText');
};
var formSubmitAction = function (url, params) {
    $.messager.progress();	// 显示进度条

    $('#form1').form('submit', {
        url: url,
        onSubmit: function (param) {
            addCommonFormParams(param);
            if ($.isNotEmpty(params)) {
                $.extend(param, params);
            }
            var isValid = $(this).form('validate');
            if (!isValid) {
                $.messager.progress('close');	// 如果表单是无效的则隐藏进度条
            }
            return isValid;	// 返回false终止表单提交
        },
        success: function (result) {
            ajaxSuccessHandler(result);
        }
    });
};
$(function () {

    $('#checkTypeId').combobox({
        url: ROOT_PATH + '/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml?code=' + CONSTANT.DictionaryTypeDefConstants.CHECK_TYPE,
        valueField: 'id',
        required: true,
        editable: false,
        onChange: handleNameChange
    });

    $('#checkLevelId').combobox({
        url: ROOT_PATH + '/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml?code=' + CONSTANT.DictionaryTypeDefConstants.CHECK_LEVEL,
        valueField: 'id',
        required: true,
        editable: false
    });

    $('#checkDeptIds').combotree({
        url: ROOT_PATH + '/permission/department/getDepartmentTree.shtml?parentId=0',
        required: true,
        multiple: true,
        cascadeCheck: false,
        editable: false,
        onChange: handleNameChange
    });

    $('#receiveDeptIds').combotree({
        url: ROOT_PATH + '/permission/department/getDepartmentTree.shtml?parentId=0',
        multiple: true,
        cascadeCheck: false,
        editable: false
    });

    $('#planStartDate').datebox({
        onChange: handleNameChange
    });

    $('#btn_save').on('click', function () {
        formSubmitAction(GLOBAL.ACTION_URL);
    });
});